import {Component, OnInit} from '@angular/core';
import {SysMenu, SysMenuCondition} from '../../sys-menu.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifySysMenuComponent} from '../modify-sys-menu.component';

@Component({
    selector: 'app-main-modify-sys-menu',
    templateUrl: './main-modify-sys-menu.component.html',
    styleUrls: ['./main-modify-sys-menu.component.css']
})
export class MainModifySysMenuComponent implements OnInit {

    errRes = new ErrRes();

    domain: SysMenu = new SysMenu();

    extend = {
        domain: {},
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: SysMenuCondition = new SysMenuCondition();
            condition.uuid = uuid;
            Services.sysMenu.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                }
            });
        },
        update: {
            req: (event) => {
                Services.sysMenu.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.sysMenu.list, {
                        moduleId: "system-management",
                        menuId: "sys-menu"
                    }]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };
    parentDomains = [];
    initParentDomains = () => {
        const search = new SysMenuCondition();
        Services.sysMenu.list(search, {
            page: 0,
            size: 200,
            orders: 'serNum-asc'
        }).subscribe(item => {
            this.parentDomains = item.page.content.filter(item => item.parentId === null);
        });
    };
    constructor(public modifySysMenu: ModifySysMenuComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.modifySysMenu.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
        this.initParentDomains();
    }

}
